import util.GuiImg;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transfer extends JPanel implements ActionListener {
    /**
     * 转账
     */
    private Icon atmIcon11 = GuiImg.getIcon("/imgs/buttonSure.jpg");
    private JButton btnlogin11 = new JButton(atmIcon11);
    private JTextField textField3 = new JTextField();
    private JTextField textField4 = new JTextField();
    private Atm frame;
    public Transfer(Atm frame){
        this.frame = frame;
        this.setLayout(null);
        //账号显示框
        JLabel textlabel3 = new JLabel("转账账号：");
        textlabel3.setBounds(180, 150, 100, 30);
        this.add(textlabel3);
        //单行输入框
        textField3.setBounds(250, 150, 250, 30);
        this.add(textField3);
        //金额显示框
        JLabel textlabel4 = new JLabel("转账金额：");
        textlabel4.setBounds(180, 200, 100, 30);
        this.add(textlabel4);
        //单行输入框
        textField4.setBounds(250, 200, 250, 30);
        this.add(textField4);
        //密码按钮
        this.setOpaque(false);
        btnlogin11.setBounds(330, 300, 100, 40);
        this.add(btnlogin11);
        btnlogin11.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        User otherUser=new User();
        User user=frame.getUserid(textField3.getText());
        Double money=frame.getLoginuser().getMoney()-Double.parseDouble(textField4.getText());
        Double money1=user.getMoney()+Double.parseDouble(textField4.getText());
        if (user==null){
            JOptionPane.showMessageDialog(this, "查无此人");
            return;
        }
        if (user.getId().equals(frame.getLoginuser().getId())){
            JOptionPane.showMessageDialog(this, "不能转给自己");
            return;
        }
        if (frame.getLoginuser().getMoney()<Double.parseDouble(textField4.getText())){
            JOptionPane.showMessageDialog(this, "余额不足");
        }
        User thisUser=frame.getUserid(frame.getLoginuser().getId());
        thisUser.setMoney(thisUser.getMoney()-Double.parseDouble(textField4.getText()));
        otherUser=frame.getUserid(textField3.getText());
        otherUser.setMoney(otherUser.getMoney()+Double.parseDouble(textField4.getText()));
        frame.zhuanzhang(thisUser,otherUser);
        JOptionPane.showMessageDialog(this, "转账成功");
        frame.switchPanel("菜单");
    }

    public JTextField getTextField3() {
        return textField3;
    }

    public void setTextField3(JTextField textField3) {
        this.textField3 = textField3;
    }

    public JTextField getTextField4() {
        return textField4;
    }

    public void setTextField4(JTextField textField4) {
        this.textField4 = textField4;
    }
}
